<?php
/*
 * 收货地址
 * 生成地址的js
 */
namespace Mobile\Controller;

class AddressController extends BaseController {
    
    public function index(){
        
        $seo_data = array(
            "title" => $this->lang['address_index'],
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
        
        $share_data = array(
            "title" => $this->lang['address_index'],
        );
        $this->setShare($share_data);

        $this->display();
    }
    
    /*
     * 创建收货地址js
     */
    public function createArea(){
        $this->_areadatajson();
    }
    
    /*
     * 创建运费js
     */
    public function createShipping(){
        $this->_expressData();
    }
    
    /*
     * 获取地址json数据
     */
    protected  function _areadatajson(){
    
        $a = D('area');
        $where_province = array(
            "level" => 1,
            "parentid" => 100000
        );
        $province = $a->where($where_province)->field("area_id,shortname as area_name")->select();//省份
        $newdata = array();
        if(empty($province)){
            return false;
        }
        $new_province = $all_city = array();
    
        //重新组装省份数据
        foreach($province as $k=>$v){
            $province_id = $v['area_id'];
            $new_province[$province_id] = $v;
        }
    
        $newdata["parent_id_0"] = $new_province;//省
    
        //遍历省份
        foreach($new_province as $k=>$v){
            $province_id = $v['area_id'];
            $where_city = array(
                "level" => 2,
                "parentid" => $province_id
            );
            $city = $a->where($where_city)->field("area_id,area_name")->select();//获取城市
            if(empty($city)){
                continue;
            }
            //重新组装城市数据
            $new_city = array();
            foreach($city as $kk=>$vv){
                $city_id = $vv['area_id'];
                $new_city[$city_id] = $vv;
                $all_city[$city_id] = $vv;//所有城市
            }
            $newdata["parent_id_".$province_id] = $new_city;
        }

        //遍历所有城市
        foreach($all_city as $kkk=>$vvv){
            $city_id = $vvv['area_id'];
            $where_country = array(
                "level" => 3,
                "parentid" => $city_id
            );
            $country = $a->where($where_country)->field("area_id,area_name")->select();//区域数据
            if(empty($country)){
                continue;
            }
            //重新组装区域数据
            $new_country = array();
            foreach($country as $kkkk=>$vvvv){
                $country_id = $vvvv['area_id'];
                $new_country[$country_id] = $vvvv;
            }
            $newdata["parent_id_".$city_id] = $new_country;
        }
    
        $area_data = json_encode($newdata);
        $area_data = "var dsy = ".$area_data.";";
        file_put_contents(ROOT."/Static/mobile/js/areadata.js", $area_data);

        echo get_domain()."/Static/mobile/js/areadata.js";
        
        
    }
    
    
    /*
     * 组装express，json
     */
    protected  function _expressData(){
        $s = M("express");
        $list = $s->select();
        $express = array();
        foreach($list as $a){
            $express[$a["province_id"]] = $a;
        }
        
        $area_data = json_encode($express);
        $area_data = "var expressData = ".$area_data.";";
        file_put_contents(ROOT."/Static/mobile/js/expressdata.js", $area_data);
        echo get_domain()."/Static/mobile/js/expressdata.js";
        
        
    }
    
    
}